﻿#region Using statements
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
#endregion

namespace SmartFrequency.Utils
{
    class Settings
    {
        #region Private variables
        string FILENAME;

        public struct ConfigItems
        {
            string m_variable, m_value;

            public string Variable
            {
                get { return m_variable; }
                set { m_variable = value; }
            }

            public string Value
            {
                get { return m_value; }
                set { m_value = value; }
            }

            public ConfigItems(string var, string val)
            {
                m_variable = var;
                m_value = val;
            }
        }

        List<ConfigItems> ConfigItemList = new List<ConfigItems>();
        #endregion

        #region Private functions
        private int Parse(string Line)
        {
            if ((Line == "") || (Line[0] == '#'))
                return -1;

            int i = Line.IndexOf("=");
            
            string var = Line.Substring(0, i);
            string val = Line.Substring(i + 1, (Line.Length - i - 1));

            ConfigItems temp = new ConfigItems(var, val);
            ConfigItemList.Add(temp);

            return 0;
        }

        private void Create()
        {
            // Defines program language
            ConfigItemList.Add(new ConfigItems("language", "tr"));

            // Defines recordnig settings
            ConfigItemList.Add(new ConfigItems("samplingrate", "22050"));
            ConfigItemList.Add(new ConfigItems("bitspersample", "16"));
            ConfigItemList.Add(new ConfigItems("channelno", "1"));
            ConfigItemList.Add(new ConfigItems("duration", "300"));
            ConfigItemList.Add(new ConfigItems("recordbuffer", "88200"));
            ConfigItemList.Add(new ConfigItems("playbuffer", "88200"));

            // Defines program general options
            ConfigItemList.Add(new ConfigItems("dblclkduration", "200"));

            // Defines file manager options
            ConfigItemList.Add(new ConfigItems("displaydirectories", "1"));

            Store();
        }
        #endregion

        #region Public functions
        public Settings(string filename)
        {
            FILENAME = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\" + filename;

            if (File.Exists(FILENAME) == false)
                Create();
        }

        // Return number of read config items
        public int Load()
        {
            StreamReader reader = null;
            string str;
            int ret = 0;

            try
            {
                reader = new StreamReader(FILENAME);
                //reader = File.OpenText(FILENAME);

                if (ConfigItemList.Count != 0)
                    ConfigItemList.Clear();

                str = reader.ReadLine();
                while (str != null)
                {
                    Parse(str);
                    str = reader.ReadLine();
                }

                ret = 0;
            }
            catch
            {
                ret = -1;
            }
            finally
            {
                if(reader != null)
                    reader.Close();
            }

            ret = ConfigItemList.Count;

            return ret;
        }

        public int Store()
        {
            StreamWriter writer = null;
            int ret = 1, i;

            string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            try
            {
                writer = File.CreateText(path + "\\temp.txt");
                for (i = 0; i < ConfigItemList.Count; i++)
                    writer.WriteLine(ConfigItemList[i].Variable + "=" + ConfigItemList[i].Value);

                writer.Close();
                writer = null;

                if (File.Exists(FILENAME) == true)
                    File.Delete(FILENAME);

                File.Move(path + "\\temp.txt", FILENAME);
            }
            catch(Exception e)
            {
                string error = e.ToString();
                ret = -1;
            }
            finally
            {
                if (writer != null)
                    writer.Close();
            }

            return ret;
        }

        public string GetMember(string member)
        {
            int i;

            for (i = 0; i < ConfigItemList.Count; i++)
                if (ConfigItemList[i].Variable == member)
                    return ConfigItemList[i].Value;

            return null;
        }

        public int SetMember(string member, string value)
        {
            int i;

            for (i = 0; i < ConfigItemList.Count; i++)
                if (ConfigItemList[i].Variable == member)
                {
                    ConfigItemList.Remove(ConfigItemList[i]);
                    ConfigItems temp = new ConfigItems(member, value);
                    ConfigItemList.Add(temp);

                    return 1;
                }

            return -1;

        }
        #endregion
    }
}
